/**
 * Created by ajhin on 1/29/2015.
 */
package com.societyGames.socialFeedExercises
{
import com.societyGames.action.Action;
import com.societyGames.system.EventObject;

import flash.external.ExternalInterface;

public class SocialFeedAction extends Action
{
  private var _responseSuccess:Boolean = false;
  private var _responseMessage:String;
  private var _beforeCompleteEventObject:EventObject;
  private static const postToFeedCallbackName:String = "onPostToFeed";

  public function SocialFeedAction()
  {
    _beforeCompleteEventObject = new EventObject(this, "BEFORE_COMPLETE");
  }

  public function get beforeCompleteEventObject():EventObject
  {
    return this._beforeCompleteEventObject;
  }

  override public function startAction():void
  {
    super.startAction();
    if (ExternalInterface.available)
    {
      ExternalInterface.addCallback(postToFeedCallbackName, onPostToFeedComplete);
      ExternalInterface.call("social.postToFeed", postToFeedCallbackName);
    }
    else
    {
      onPostToFeedComplete(true, "Could not contact Facebook.");
    }
  }

  public function get responseSuccess():Boolean
  {
    return this._responseSuccess;
  }

  public function get responseMessage():String
  {
    return this._responseMessage;
  }

  private function onPostToFeedComplete(success:Boolean, message:String):void
  {
    if (ExternalInterface.available)
    {
      ExternalInterface.addCallback(postToFeedCallbackName, null);
    }
    _responseSuccess = success;
    _responseMessage = message;
    this._beforeCompleteEventObject.dispatchEvent();
    this._completeEventObject.dispatchEvent(); //For now, just complete immediately.
  }
}
}
